package com.setting.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Ope;
import com.entity.Sysdic;
import com.interfaces.OpeDao;
import com.login.entity.JsonLogin;
import com.setting.entity.JsonSys;
import com.utils.CglibBeanCopierUtils;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: OpeService.java
 * @Package com.setting.service
 * @author 孙梦楠
 * @date 2017年11月3日 下午7:46:01
 * @version V1.0.0
 */
@Service
public class OpeService {

	@Autowired
	private OpeDao opedao;

	@Autowired
	private SysdicService sysdicservice;

	// 展示所有
	@Transactional
	public JsonResult<List<JsonLogin>> listopes(String languagecookie) {
		List<JsonLogin> jsonlogins = new ArrayList<JsonLogin>();
		List<Ope> list = opedao.findAll();
		for (Ope ope : list) {
			JsonLogin jsonLogin = dojson(languagecookie, ope);
			jsonlogins.add(jsonLogin);
		}

		return new JsonResult<List<JsonLogin>>(771, "展示所有", jsonlogins);
	}

	// 增加
	@Transactional
	public JsonResult<Ope> addope(String opecode, String opename, Integer opeorg, String opeurl, String opetype,
			String opemenu, String opephone) {

		if (Utils.isNullOrEmpty(opecode) || Utils.isNullOrEmpty(opename) || Utils.isNullOrEmpty(opetype)
				|| Utils.isNullOrEmpty(opemenu) || Utils.isNullOrEmpty(opephone) || opeorg == null) {
			return new JsonResult<Ope>(772, "增加操作员失败..数值不能为空", null);
		} else {
			Ope ope = new Ope();
			ope.setOpecode(opecode);
			ope.setOpepassword("00000000");// 初始密码
			ope.setOpeverification("111111");
			ope.setOpename(opename);
			ope.setOpeorg(opeorg);
			ope.setOpeurl(opeurl);
			ope.setOpeisdel((byte) 0);
			ope.setOpetype(opetype);
			ope.setOpemenu(opemenu);
			ope.setOpephone(opephone);
			opedao.save(ope);
			return new JsonResult<Ope>(773, "增加操作员成功", ope);
		}
	}

	// 删除
	@Transactional
	public JsonResult<Ope> delectope(Integer opeid) {
		if (opeid != null) {
			opedao.updateOpeisdel((byte) 1, opeid);
			return new JsonResult<Ope>(774, "删除成功", null);
		} else
			return new JsonResult<Ope>(775, "删除失败", null);
	}

	// 修改界面
	@Transactional
	public JsonResult<JsonLogin> updateviewope(Integer opeid, String languagecookie) {
		if (opeid != null) {
			Ope ope = opedao.findOne(opeid);
			JsonLogin jsonLogin = dojson(languagecookie, ope);
			return new JsonResult<JsonLogin>(776, "查单个操作员", jsonLogin);
		} else
			return new JsonResult<JsonLogin>(777, "查询失败..ID为空", null);
	}

	// 修改
	@Transactional
	public JsonResult<Ope> updateope(Integer opeid, String opecode, String opename, Integer opeorg, String opeurl,
			String opetype, String opemenu, String opephone) {
		Ope ope = new Ope();

		if (opeid == null || Utils.isEmpty(opecode) || Utils.isEmpty(opename) || Utils.isEmpty(opetype)
				|| Utils.isEmpty(opemenu) || Utils.isEmpty(opephone)) {
			return new JsonResult<Ope>(778, "修改管理员失败..数值不能为空", null);
		} else {
			ope.setOpeid(opeid);
			ope.setOpepassword(opedao.findOne(opeid).getOpepassword());
			ope.setOpeisdel((byte) 0);
			ope.setOpeverification("111111");
			// 判断opename
			if (Utils.isNull(opecode)) {
				ope.setOpecode(opedao.findOne(opeid).getOpecode());

			} else {
				ope.setOpecode(opecode);
			}

			// 判断opename
			if (Utils.isNull(opename)) {
				ope.setOpename(opedao.findOne(opeid).getOpename());

			} else {
				ope.setOpename(opename);
			}
			// 判断opename
			if (Utils.isNull(opeurl)) {
				ope.setOpeurl(opedao.findOne(opeid).getOpeurl());

			} else {
				ope.setOpeurl(opeurl);
			}
			// 判断opename
			if (Utils.isNull(opetype)) {
				ope.setOpetype(opedao.findOne(opeid).getOpetype());

			} else {
				ope.setOpetype(opetype);
			}
			// 判断opename
			if (Utils.isNull(opemenu)) {
				ope.setOpemenu(opedao.findOne(opeid).getOpemenu());

			} else {
				ope.setOpemenu(opemenu);
			}
			// 判断opename
			if (Utils.isNull(opephone)) {
				ope.setOpephone(opedao.findOne(opeid).getOpephone());

			} else {
				ope.setOpephone(opephone);
			}
			// 判断opename
			if (opeorg == null) {
				ope.setOpeorg(opedao.findOne(opeid).getOpeorg());

			} else {
				ope.setOpeorg(opeorg);
			}

			opedao.save(ope);
			return new JsonResult<Ope>(779, "修改操作员成功", ope);
		}
	}
	
	//修改操作员密码
	@Transactional
	public JsonResult<Ope> updatepassword(Integer opeid,String opepassword,String newpassword,String newpassword2) {
		if(!(opedao.findOne(opeid).getOpepassword().equals(opepassword))) {
			return new JsonResult<Ope>(780, "修改密码失败..密码错误", null);	
		}
		else if (!(newpassword.equals(newpassword2))) {
			return new JsonResult<Ope>(781, "修改密码失败..两次密码输入不一致", null);	
		} else {
			opedao.updatePassword(newpassword, opeid);
			return new JsonResult<Ope>(782, "修改密码成功", null);	
		}
	}

	private JsonLogin dojson(String languagecookie, Ope ope) {
		JsonLogin jsonLogin = new JsonLogin();
		CglibBeanCopierUtils.copyProperties(ope, jsonLogin);
		jsonLogin.setMenuname(sysdicservice.finddisplay("0030", ope.getOpemenu(), languagecookie));
		jsonLogin.setTypename(sysdicservice.finddisplay("0006", ope.getOpetype(), languagecookie));
		jsonLogin.setOrgname(sysdicservice.finddisplay("0001", ope.getOpeorg().toString(), languagecookie));
		return jsonLogin;
	}

}
